package com.cpi.cornpp.customer.services;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.customer.converter.CustomerConverter;
import com.cpi.cornpp.customer.dao.CustomerDAO;
import com.cpi.cornpp.customer.dto.CustomerDTO;
import com.cpi.cornpp.customer.dto.CustomerForecastDTO;
import com.cpi.cornpp.customer.exception.CustomerException;
import com.cpi.cornpp.customer.view.CustomerForecastView;
import com.cpi.cornpp.customer.view.CustomerView;

public class CustomerServices {

	public CustomerView getCustomerById(String customerId)throws CustomerException {
		CustomerDAO customerDAO = null;
		CustomerConverter customerConverter = null;
		CustomerDTO customerDTO = null;
		CustomerView customerView = null;
		try{
			customerDAO = new CustomerDAO();
			customerConverter = new CustomerConverter();
			customerDTO = customerDAO.getCustomerById(new CustomerDTO(customerId));
			customerView = customerConverter.convert(customerDTO);
		}catch (CustomerException customerException) {			
			customerException.printStackTrace(System.out);
			customerException= new CustomerException(customerException , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw customerException;

		}
		catch (Exception e){
			CustomerException customerException=null;
			e.printStackTrace(System.out);
			customerException= new CustomerException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw customerException;
		}
		return customerView;
	}
	
	public Collection getCustomersList(CustomerView customerView) throws CustomerException {
		CustomerDAO customerDAO = null;
		CustomerConverter customerConverter = null;
		CustomerDTO customerDTO = null;
		Collection collectionCustomerDTO = null;
		Collection collectionCustomerView = null;
		try {
			customerDAO = new CustomerDAO();
			customerDTO = new CustomerDTO();
			customerConverter = new CustomerConverter();
			collectionCustomerDTO = new ArrayList(0);
			collectionCustomerView = new ArrayList(0);
			
			// La vista la convierto en un DTO
			customerDTO = customerConverter.convert(customerView);
			// Obtengo el listado de los clientes
			collectionCustomerDTO = customerDAO.getCustomers(customerDTO);
			// Mando convertir el listado en la vista de retorno
			collectionCustomerView = customerConverter.convertDTOs(collectionCustomerDTO);
		} catch (CustomerException customerException) {			
			customerException.printStackTrace(System.out);
			customerException= new CustomerException(customerException , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw customerException;

		}
		catch (Exception e){
			CustomerException customerException=null;
			e.printStackTrace(System.out);
			customerException= new CustomerException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw customerException;
		}
		return collectionCustomerView;
	}
	
	public Collection getCustomers(CustomerForecastView customerForecastView) throws CustomerException {
		 Collection customerViews = null;
		 List customerDTOs = null;
		 CustomerDAO customerDAO =null;
		 CustomerConverter customerConverter = null;
		 CustomerForecastDTO customerForecastDTO = null;
		 try{
			 customerDAO = new CustomerDAO();
			 customerConverter = new CustomerConverter();
			 customerForecastDTO = customerConverter.convertForListOfGroup(customerForecastView);
			 customerDTOs = customerDAO.getCustomersForListOfGroup(customerForecastDTO);
			 customerViews = customerConverter.convertForecastDTOs(customerDTOs);
		 }catch(CustomerException customerException){
			 throw customerException;
		 }catch(Exception exception){
			CustomerException customerException=null;
			exception.printStackTrace(System.out);
			customerException= new CustomerException(exception , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw customerException;
		 }
		 return customerViews;
	}
}